layui.use(['form', 'jquery', 'layer', 'table', 'laydate'], function() {
	var form = layui.form;
	var $ = layui.$;
	var table = layui.table;
	var layer = layui.layer;
	var laydate = layui.laydate;
	var partner_id = '';
	if(partner_form_value.partner_id != undefined) {
		partner_id = partner_form_value.partner_id;
		if(partner_form_value.end_time == '买断') {
			partner_form_value.maiduan = true;
			$('.end_time').hide();
			partner_form_value.end_time = '';
		} else {
			partner_form_value.maiduan = false;
			$('.end_time').show();
		}
	}
	var lay_filter = 'tpl_partner' + partner_id;
	form.val(lay_filter, partner_form_value);
	
	// 时间选择器
	laydate.render({
		elem: 'form[lay-filter="' + lay_filter + '"] input[name="start_time"]'
	});
	// 时间选择器
	laydate.render({
		elem: 'form[lay-filter="' + lay_filter + '"] input[name="end_time"]'
	})

	// 表单渲染
	form.render('checkbox');

	// 监听复选框
	form.on('checkbox', function(data) {
		var check_name = $(this).attr('name');
		var value = data.elem.checked;
		if(value == true) {
			$(this).parents('.layui-form-item').next('.layui-form-item').hide();
			$('input[name="end_start"]').val('');
		} else {
			$(this).parents('.layui-form-item').next('.layui-form-item').show();
		}
	})
	// 监听提交
	form.on('submit(submit_partner)', function(data) {
		var field = data.field;
		var url = $('form[lay-filter="' + lay_filter + '"]').attr('action');
		if(field.maiduan == undefined) {
			if(field.end_time == '') {
				layer.msg('合作结束时间必填', {
					icon: 2,
					anim: 6
				});
				$('input[name="end_time"]').addClass('layui-form-danger').focus();
				return false;
			}
		}
		$.post(url, field, function(data) {
			if(data.code == 0) {
				layer.msg('保存成功', {
					icon: 1
				});
				// 关闭当前页 刷新父页
				$('.layui-body .layui-tab[lay-filter="content"] li.layui-this .layui-tab-close').trigger('click');
				$('.layui-body .layui-tab[lay-filter="content"] li[lay-id="partner"]').trigger('click');
				$('.layui-body .layui-tab[lay-filter="content"] li[lay-id="partner_list"]').trigger('click');
				// 表格重载
				if(field.partner_id == undefined || field.partner_id == ''){
					table.reload('partner_list');
				} else {
					table.editTr('partner_list','partner_id',field.partner_id);
				}
			} else {
				layer.msg(data.msg, {
					icon: 2,
					anim: 6
				});
			}
		}, 'json')
		return false;
	})

})